const router=require('koa-router')();
const request=require('request');

var URL={
	movieList:'https://api.douban.com/v2/movie/in_theaters',
	movieDetail:'https://api.douban.com/v2/movie/subject',
	movieSoon:'https://api.douban.com/v2/movie/coming_soon'
}

router.get('/api/in_theaters',async (ctx)=>{
	ctx.type='json';
	ctx.body= await _fn.getData(URL.movieList);
})

router.get('/api/coming_soon',async (ctx)=>{
	ctx.type='json';
	ctx.body= await _fn.getData(URL.movieSoon);
})

router.get('/api/subject/:id',async (ctx)=>{
	ctx.type='json';
	ctx.body=await _fn.getData(URL.movieDetail+'/'+ctx.params.id);
});

var _fn={
	getData:function(url){
		return new Promise(function(resolve,reject){
			request({
				url:url,
				method:'GET'
			},function(error,response,body){
				if(!error && response.statusCode==200){
					resolve(body);
				}
			});
		})
	}
}

module.exports=router;